// react redux 数据传递
import { createStore, combineReducers } from "redux";
// 数据可视化化
import { persistStore, autoRehydrate } from "redux-persist";

 const { asyncSessionStorage } = require("redux-persist/storages");
// 创建 stare
const initialState = {
  number: 0,
};
const loading = {
  loading: false,
};
// 做一些事情(模块A)
const doSomethingWithA = (state = initialState, action) => {
  // state 初始化一些数据  action 触发事件
  console.log(state, action, "===========state,action");
  switch (action.type) {
    // case actionTypes.ADD_TEST:
    case "1":
      return {
        ...state,
        number: action.number + 1,
      };
    default:
      return state;
  }
};
// 做一些事情(模块B)
const doSomethingWithB = (state = initialState, action) => {
  // state 初始化一些数据  action 触发事件
  switch (action.type) {
    // case actionTypes.ADD_TEST:
    case "2":
      return {
        ...state,
        number: action.number + 2,
      };
    default:
      return state;
  }
};

// 做一些事情(请求loading)
const doSomethingWithC = (state = loading, action) => {
  // state 初始化一些数据  action 触发事件
  switch (action.type) {
    // case actionTypes.ADD_TEST:
    case "loading":
      return {
        ...state,
        loading: action.loading,
      };
    default:
      return state;
  }
};

// 多数据合并
const reducers = combineReducers({
  a: doSomethingWithA,
  b: doSomethingWithB,
  loading: doSomethingWithC,
});

// 等同于
// function reducer(state = {}, action) {
//   return {
//     // 类似 vuex module模块
//     a: doSomethingWithA(state.a, action),
//     b: processB(state.b, action),
//     c: c(state.c, action),
//   };
// }

//vue data 函数创建不同的 store  覆盖默认值
// const store = createStore(reducers, { a: { number: 33 } });
const store = createStore(reducers, { a: { number: 33 } }, autoRehydrate());

//  persistStore的使用，可配置很多参数，如存储引擎、黑白名单等。
persistStore(store, {
  storage: asyncSessionStorage,
  blacklist: [
    "routing",
    "school", // school reducer 的数据不进行持久化
    "a",
  ],
});

// 初始的方法 获取 更新 处理
console.log(store, "====store");

//vue watch方法 改变监听(定时器 )
let time = store.subscribe(function (res) {
  console.log(res, store.getState(), "===subscribe");
});
time(); //删除功能

// 调用一次
store.dispatch({
  // 更新了    //方法处理
  // type: actionTypes.ADD_TEST,
  type: "1",
  number: store.getState().a.number,
});

// https://www.redux.org.cn/

export default store;


//  redux-persist 4.01   参考:https://www.jianshu.com/p/a0406d72b19b
// const { browserHistory } = require("react-router");
// const { syncHistoryWithStore, routerReducer } = require("react-router-redux");
// const { createStore, combineReducers } = require("redux");
// const { student, school } = require("./reducers");
// //  只要用上 persistStore 和 autoRehydrate 就行啦
// const { persistStore, autoRehydrate } = require("redux-persist");
// //  存储机制，可换成cookie等其他机制
// const { asyncSessionStorage } = require("redux-persist/storages");

// const initialState = {
//   student: {
//     age: 21,
//     sex: "female",
//   },
//   school: {
//     name: "蓝翔大学",
//   },
// };

// const reducers = combineReducers({
//   student,
//   school,
//   routing: routerReducer,
// });

// //  autoRehydrate作为参数传入redux的createStore方法
// const store = createStore(reducers, initialState, autoRehydrate());

// //  persistStore的使用，可配置很多参数，如存储引擎、黑白名单等。
// persistStore(store, {
//   storage: asyncSessionStorage,
//   blacklist: [
//     "routing",
//     "school", // school reducer 的数据不进行持久化
//   ],
// });

// const history = syncHistoryWithStore(browserHistory, store);

// module.exports = {
//   store,
//   history,
// };



//  redux-persist 5.01 参考:https://stackoverflow.com/questions/48514147/rnredux-persist-autorehydrate-is-not-a-function
// import React, {Component} from 'react';
// import {Provider} from 'react-redux';
// import {createStore, applyMiddleware, compose} from 'redux';
// import {PersistGate} from 'redux-persist/lib/integration/react';
// import {persistStore, persistReducer} from 'redux-persist';
// import storage from 'redux-persist/lib/storage';
// import Thunk from 'redux-thunk';
// import Router from './Router';
// import reducers from './reducers';

// const persistConfig = {
//     key: 'root',
//     storage: storage,
// };
// const persistedReducer = persistReducer(persistConfig, reducers);

// const store = compose(persistedReducer, {}, composeEnhancers(applyMiddleware(Thunk)));

// class App extends Component {
//     render() {
//         const persistor = persistStore(store);
//         return (
//             <Provider store={store}>
//                 <PersistGate persistor={persistor}>
//                     <Router />
//                 </PersistGate>
//             </Provider>
//         );
//     }
// }

// export default App;



// configureStore.js


//参考: https://github.com/rt2zz/redux-persist react 6.0.0
// import { createStore } from 'redux'
// import { persistStore, persistReducer } from 'redux-persist'
// import storage from 'redux-persist/lib/storage' // defaults to localStorage for web

// import rootReducer from './reducers'

// const persistConfig = {
//   key: 'root',
//   storage,
// }

// const persistedReducer = persistReducer(persistConfig, rootReducer)

// export default () => {
//   let store = createStore(persistedReducer)
//   let persistor = persistStore(store)
//   return { store, persistor }
// }


// import { PersistGate } from 'redux-persist/integration/react'

// // ... normal setup, create store and persistor, import components etc.

// const App = () => {
//   return (
//     <Provider store={store}>
//       <PersistGate loading={null} persistor={persistor}>
//         <RootComponent />
//       </PersistGate>
//     </Provider>
//   );
// };
